package com.automation.ljf.strategy.impl;

import com.automation.ljf.domain.dataobject.CaseStepDO;
import com.automation.ljf.strategy.OperationStrategy;
import com.automation.ljf.util.ResultMessage;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.stereotype.Component;

/**
 * 模拟键盘操作
 *
 * @author lijiefeng
 * @since 2021/8/25
 */

public class KeyboardStrategyImpl{

    @Component("CTRL_C")
    static class CopyStrategy implements OperationStrategy{
        @Override
        public ResultMessage doOperation(ChromeDriver driver, CaseStepDO caseStepDO, String jsonData) {
            String windowHandle = driver.getWindowHandle();
            try{
                WebElement we = driver.findElement(By.xpath(caseStepDO.getStepXpath()));
                we.sendKeys(Keys.CONTROL, "c");
            }catch (Exception e){
                return ResultMessage.error("测试用例" + caseStepDO.getCaseId() + "的步骤" + caseStepDO.getStepId() + "出现异常")
                        .addData("errorMsg", "不可预知的错误").addData("window", windowHandle);
            }
            return ResultMessage.ok("操作成功").addData("window", windowHandle);
        }
    }

    @Component("CTRL_A")
    static class SelectAllStrategy implements OperationStrategy{
        @Override
        public ResultMessage doOperation(ChromeDriver driver, CaseStepDO caseStepDO, String jsonData) {
            String windowHandle = driver.getWindowHandle();
            try{
                WebElement we = driver.findElement(By.xpath(caseStepDO.getStepXpath()));
                we.sendKeys(Keys.CONTROL, "a");
            }catch (Exception e){
                return ResultMessage.error("测试用例" + caseStepDO.getCaseId() + "的步骤" + caseStepDO.getStepId() + "出现异常")
                        .addData("errorMsg", "不可预知的错误").addData("window", windowHandle);
            }
            return ResultMessage.ok("操作成功").addData("window", windowHandle);
        }
    }

    @Component("CTRL_V")
    static class PasteStrategy implements OperationStrategy{
        @Override
        public ResultMessage doOperation(ChromeDriver driver, CaseStepDO caseStepDO, String jsonData) {
            String windowHandle = driver.getWindowHandle();
            try{
                WebElement we = driver.findElement(By.xpath(caseStepDO.getStepXpath()));
                we.sendKeys(Keys.CONTROL, "v");
            }catch (Exception e){
                return ResultMessage.error("测试用例" + caseStepDO.getCaseId() + "的步骤" + caseStepDO.getStepId() + "出现异常")
                        .addData("errorMsg", "不可预知的错误").addData("window", windowHandle);
            }
            return ResultMessage.ok("操作成功").addData("window", windowHandle);
        }
    }

    @Component("CTRL_X")
    static class CutStrategy implements OperationStrategy{
        @Override
        public ResultMessage doOperation(ChromeDriver driver, CaseStepDO caseStepDO, String jsonData) {
            String windowHandle = driver.getWindowHandle();
            try{
                WebElement we = driver.findElement(By.xpath(caseStepDO.getStepXpath()));
                we.sendKeys(Keys.CONTROL, "x");
            }catch (Exception e){
                return ResultMessage.error("测试用例" + caseStepDO.getCaseId() + "的步骤" + caseStepDO.getStepId() + "出现异常")
                        .addData("errorMsg", "不可预知的错误").addData("window", windowHandle);
            }
            return ResultMessage.ok("操作成功").addData("window", windowHandle);
        }
    }


}
